package com.game.Maze.Renderers;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.game.Maze.Util.DimesionsFactory;

/**
 * Объект, реализующий камеру
 * 
 */
public class CameraRenderer implements IRenderer, ITransformRenderer
{
  private OrthographicCamera m_Camera = null;
  private Vector2 m_CenterPoint;
  
  // Высота камеры
  private final static float m_CameraZ = 1.0f;

  @Override
  public void create(int aColumn, int aRow)
  {
    float cameraSize = DimesionsFactory.instance().cameraSize();
    m_Camera = new OrthographicCamera(cameraSize, cameraSize);   
  }

  @Override
  public void render(SpriteBatch batch)
  {
    m_Camera.update();
    m_Camera.apply(Gdx.gl10);
  }

  @Override
  public void resize(int width, int height)
  {   
    float cameraSize = DimesionsFactory.instance().cameraSize();
    float aspectRatio = (float) width / (float) height;
    float viewportWidth;
    float viewportHeight;
    
    if (width > height)
    {
      // Горизонтальное расположение
      viewportWidth = cameraSize * aspectRatio;
      viewportHeight = cameraSize;
    }
    else
    {
      // Вертикальное расположение
      viewportWidth = cameraSize;
      viewportHeight = cameraSize / aspectRatio;
    } 

    m_Camera.viewportWidth = viewportWidth;
    m_Camera.viewportHeight = viewportHeight;
  }

  @Override
  public void moveTo(int aColumn, int aRow)
  {
    // Получаем точку середины объекта
    m_CenterPoint = DimesionsFactory.instance().startPoint(aColumn, aRow);
    m_Camera.position.set(m_CenterPoint.x, m_CenterPoint.y, m_CameraZ);
  }
}
